/**
 @file ctc_sc_oam.h

 @date 2019-4-12

 @version v6.0

 This file contains SPN Channel OAM related data structure, enum, macro.

*/

#ifndef _CTC_SC_OAM_H
#define _CTC_SC_OAM_H
#ifdef __cplusplus
extern "C" {
#endif
/****************************************************************
 *
 * Header Files
 *
 ***************************************************************/
#include "sal_types.h"
#include "ctc_const.h"

/****************************************************************
*
* Defines and Macros
*
****************************************************************/
#define CTC_SC_OAM_EVENT_MAX_NUM 32
/**
 @defgroup sc_oam SC_OAM
 @{
*/

/**
 @brief   Define SPN Channel OAM type
*/
#define CTC_SC_OAM_TYPE_BAS  0  /**< [TMM] OAM type BAS  */
#define CTC_SC_OAM_TYPE_APS  1  /**< [TMM] OAM type APS */
#define CTC_SC_OAM_TYPE_CV   2  /**< [TMM] OAM type CV */
#define CTC_SC_OAM_TYPE_1DM  3  /**< [TMM] OAM type 1DM */
#define CTC_SC_OAM_TYPE_2DM  4  /**< [TMM] OAM type 2DM */
#define CTC_SC_OAM_TYPE_CS   5  /**< [TMM] OAM type CS */
#define CTC_SC_OAM_TYPE_MAX  6

/**
 @brief   Define SPN Channel OAM period
*/
enum ctc_sc_oam_period_e
{
    CTC_SC_OAM_PERIOD_16K_BLOCK   = 0,   /**< [TMM] Period of BAS, 16K blocks with one BAS */
    CTC_SC_OAM_PERIOD_32K_BLOCK,         /**< [TMM] Period of BAS, 32K blocks with one BAS */
    CTC_SC_OAM_PERIOD_64K_BLOCK,         /**< [TMM] Period of BAS, 64K blocks with one BAS */
    CTC_SC_OAM_PERIOD_512K_BLOCK,        /**< [TMM] Period of BAS, 512K blocks with one BAS */
    CTC_SC_OAM_PERIOD_MAX
};
typedef enum ctc_sc_oam_period_e ctc_sc_oam_period_t;

/**
 @brief   Define SPN Channel OAM MEP flag
*/
enum ctc_sc_oam_mep_flag_e
{
    CTC_SC_OAM_MEP_FLAG_MEP_EN         = (1<<0),  /**< [TMM] If set, indicate MEP is active */
    CTC_SC_OAM_MEP_FLAG_BIP_CHK_EN     = (1<<1),  /**< [TMM] If set, indicate BIP check enable */
    CTC_SC_OAM_MEP_FLAG_REI_CHK_EN     = (1<<2)   /**< [TMM] If set, indicate REI check enable */
};
typedef enum ctc_sc_oam_mep_flag_e ctc_sc_oam_mep_flag_t;

/**
 @brief   Define SPN Channel OAM bits error rate threshold
*/
struct ctc_sc_oam_ber_thrd_s
{
    uint32 ber_detect_time;       /**< [TMM] Indeicate how many T(BAS RX period) for ber check */
    uint32 ddeg_set_thrd;         /**< [TMM] Bits error rate threshold for dDEG(degraded signal defect) detect,
                                             unit:10^(-13), e.g., if ddeg_detect_thrd = 200, the bits error rate threshold is 2*(10^(-11))*/
    uint32 ddeg_clear_thrd;       /**< [TMM] Bits error rate threshold for dDEG(degraded signal defect) clear */
    uint32 dexc_set_thrd;         /**< [TMM] Bits error rate hreshold for dEXC(excessive error defect) detect */
    uint32 dexc_clear_thrd;       /**< [TMM] Bits error rate threshold for dEXC(excessive error defect) detect */
};
typedef struct ctc_sc_oam_ber_thrd_s ctc_sc_oam_ber_thrd_t;

/**
 @brief   Define SPN Channel OAM APS infomation
*/
struct ctc_sc_oam_aps_info_s
{
    uint8 aps_en;               /**< [TMM] If set, means run APS state machine and do APS swith. Valid when set CTC_SC_OAM_PROP_HW_APS_SWTICH */
    uint8 is_protection;        /**< [TMM] If set, indicate the MEP is on the protection path. Valid when set CTC_SC_OAM_PROP_HW_APS_SWTICH */
    uint8 sc_aps_en;            /**< [TMM] If set, means SPN channel APS. Else, The grp_id must be same as aps_bridge_group_id in APS module,
                                       and the working and protection port are specified by APS module. Valid when set CTC_SC_OAM_PROP_HW_APS_SWTICH */
    uint8 wtr_timer;            /**< [TMM] Wait-to-restore (WTR) period, unit: 1min, only config on Working MEP. Valid when set CTC_SC_OAM_PROP_HW_APS_SWTICH */
    uint32 w_client_id;          /**< [TMM] Valid when set is_protection, means the working client which to bind. Valid when set CTC_SC_OAM_PROP_HW_APS_SWTICH */
    uint16 aps_bridge_group_id; /**< [TMM] Valid when sc_aps_en = 0, only config on working MEP. Valid when set CTC_SC_OAM_PROP_HW_APS_SWTICH */

    uint8 request_state;        /**< [TMM] Request/state defined in G.8031 */
    uint8 protection_type;      /**< [TMM] Protection type defined in G.8031 */
    uint8 requested_signal;     /**< [TMM] Requested signal defined in G.8031 */
    uint8 bridged_signal;       /**< [TMM] Bridged signal defined in G.8031 */
    uint8 last_state;           /**< [TMM] Last/state defined in G.8031 */
    uint8 current_state;    /**< [TMM] Current/state defined in G.8031 */
};
typedef struct ctc_sc_oam_aps_info_s ctc_sc_oam_aps_info_t;

struct ctc_sc_oam_ber_cnt_s
{
    uint32 bip_cnt;         /**< [TMM] BIP count during BAS RX period */
    uint32 rei_cnt;         /**< [TMM] REI count during BAS RX period */
};
typedef struct ctc_sc_oam_ber_cnt_s ctc_sc_oam_ber_cnt_t;

/**
 @brief   Define SPN Channel OAM MEP
*/
struct ctc_sc_oam_mep_s
{
    uint32 client_id;                  /**< [TMM] MEP key for SPN channel OAM */

    uint32 flag;                       /**< [TMM] Local MEP flag, refer to ctc_sc_oam_mep_flag_t */
    uint16 client_speed;               /**< [TMM] Client speed, unit:1G, must multiple of 5G. */

    uint32 rx_en_bmp;                  /**< [TMM] Rx process enable if set corresponding type, refer to CTC_SC_OAM_TYPE_XXX */
    uint32 tx_en_bmp;                  /**< [TMM] Tx process enable if set corresponding type, refer to CTC_SC_OAM_TYPE_XXX */

    uint8 bas_rx_period;               /**< [TMM] refer to ctc_sc_oam_period_t */
    uint8 bas_tx_period;               /**< [TMM] refer to ctc_sc_oam_period_t */
    uint8 bas_seq_mode;                /**< [TMM] Sequence mode for BAS, 0: cyclic value for 0 to 15; 1: fixed value */
    uint8 bas_seq;                     /**< [TMM] Sequence value for BAS when bas_seq_mode is fixed mode */
    uint8 cs_lpi;                      /**< [TMM] Local CS_LPI value for BAS TX */
    uint8 cs_lf;                       /**< [TMM] Local CS_LF value for BAS TX */
    uint8 cs_rf;                       /**< [TMM] Local CS_RF value for BAS TX */
    uint8 rdi;                         /**< [TMM] Local RDI value for BAS TX */
    ctc_sc_oam_ber_thrd_t ber_thrd;    /**< [TMM] Threshold for BER(Bits Error Rate) detect */
    ctc_sc_oam_ber_thrd_t rei_thrd;    /**< [TMM] Threshold for REI(Remote Error Indicate) detect */

    uint8 cv_interval;                 /**< [TMM] CV TX interval, unit: one second, default 10s */
    uint8 sapi[16];                    /**< [TMM] SAPI */
    uint8 dapi[16];                    /**< [TMM] DAPI */

    uint8 one_dm_interval;             /**< [TMM] 1DM TX interval, unit: one second, default 10s */
    uint8 two_dm_interval;             /**< [TMM] DMM TX interval, unit: one second, default 1s */

    uint8 cs_interval;                 /**< [TMM] CS TX interval, unit: one second, default 10s */
    uint8 cs_type;                     /**< [TMM] CS type */

    uint8 aps_interval;                /**< [TMM] APS TX interval, unit: one second, default 1s */
};
typedef struct ctc_sc_oam_mep_s ctc_sc_oam_mep_t;

/**
 @brief   Define APS Local Command or Event
*/
enum ctc_sc_oam_aps_cmd_e
{
    CTC_SC_OAM_APS_CMD_LO,             /**< [TMM] Lockout of protection */
    CTC_SC_OAM_APS_CMD_FS,             /**< [TMM] Forced switch */
    CTC_SC_OAM_APS_CMD_MS_TO_P,        /**< [TMM] Manual switch to protection*/
    CTC_SC_OAM_APS_CMD_MS_TO_W,        /**< [TMM] Manual switch to woking*/
    CTC_SC_OAM_APS_CMD_EXER,           /**< [TMM] Exercise */
    CTC_SC_OAM_APS_CMD_CLEAR,          /**< [TMM] Clear */
    CTC_SC_OAM_APS_CMD_FREEZE,         /**< [TMM] Freezes the state of the protection group */
    CTC_SC_OAM_APS_CMD_CLEAR_FREEZE,   /**< [TMM] Clears the Freeze command */

    CTC_SC_OAM_APS_CMD_SF,             /**< [TMM] Signal Fail */
    CTC_SC_OAM_APS_CMD_RSF,            /**< [TMM] Recover from SF */
    CTC_SC_OAM_APS_CMD_SD,             /**< [TMM] Signal Degrade */
    CTC_SC_OAM_APS_CMD_RSD,            /**< [TMM] Recover from SD */
    CTC_SC_OAM_APS_CMD_MAX
};
typedef enum ctc_sc_oam_aps_cmd_e ctc_sc_oam_aps_cmd_t;

/**
 @brief   Define SPN Channel OAM local MEP update type
*/
enum ctc_sc_oam_mep_prop_type_e
{
    CTC_SC_OAM_MEP_PROP_MEP_EN,            /**< [TMM] MEP enable */
    CTC_SC_OAM_MEP_PROP_CLIENT_SPEED,      /**< [TMM] Client speed, unit:1G, must multiple of 5G */
    CTC_SC_OAM_MEP_PROP_RX_EN_BMP,         /**< [TMM] Rx process enable if set corresponding type, refer to CTC_SC_OAM_TYPE_XXX */
    CTC_SC_OAM_MEP_PROP_TX_EN_BMP,         /**< [TMM] Tx process enable if set corresponding type, refer to CTC_SC_OAM_TYPE_XXX */
    CTC_SC_OAM_MEP_PROP_BIP_CHK_EN,        /**< [TMM] BIP check enable*/
    CTC_SC_OAM_MEP_PROP_REI_CHK_EN,        /**< [TMM] REI check enable*/
    CTC_SC_OAM_MEP_PROP_BAS_RX_PERIOD,     /**< [TMM] refer to ctc_sc_oam_period_t */
    CTC_SC_OAM_MEP_PROP_BAS_TX_PERIOD,     /**< [TMM] refer to ctc_sc_oam_period_t */
    CTC_SC_OAM_MEP_PROP_BAS_SEQ_MODE,      /**< [TMM] Sequence mode for BAS, 0: cyclic value for 0 to 15; 1: fixed value */
    CTC_SC_OAM_MEP_PROP_BAS_SEQ,           /**< [TMM] Sequence value for BAS when bas_seq_mode is fixed mode */
    CTC_SC_OAM_MEP_PROP_BER_THRD,          /**< [TMM] Threshold for BER detect, refer to ctc_sc_oam_ber_thrd_t, use p_update_value */
    CTC_SC_OAM_MEP_PROP_REI_THRD,          /**< [TMM] Threshold for REI detect, refer to ctc_sc_oam_ber_thrd_t, use p_update_value */
    CTC_SC_OAM_MEP_PROP_CS_LPI,            /**< [TMM] Local CS_LPI value for BAS TX */
    CTC_SC_OAM_MEP_PROP_CS_LF,             /**< [TMM] Local CS_LF value for BAS TX  */
    CTC_SC_OAM_MEP_PROP_CS_RF,             /**< [TMM] Local CS_RF value for BAS TX  */
    CTC_SC_OAM_MEP_PROP_RDI,               /**< [TMM] Local RDI value for BAS TX*/
    CTC_SC_OAM_MEP_PROP_CV_INTERVAL,       /**< [TMM] CV TX interval, unit: one second */
    CTC_SC_OAM_MEP_PROP_SAPI,              /**< [TMM] SAPI, use p_update_value */
    CTC_SC_OAM_MEP_PROP_DAPI,              /**< [TMM] DAPI, use p_update_value  */
    CTC_SC_OAM_MEP_PROP_1DM_INTERVAL,      /**< [TMM] 1DM TX interval, unit: one second */
    CTC_SC_OAM_MEP_PROP_2DM_INTERVAL,      /**< [TMM] DMM TX interval, unit: one second */
    CTC_SC_OAM_MEP_PROP_CS_INTERVAL,       /**< [TMM] CS TX interval, unit: one second */
    CTC_SC_OAM_MEP_PROP_CS_TYPE,           /**< [TMM] CS type */

    CTC_SC_OAM_MEP_PROP_APS_INTERVAL,      /**< [TMM] APS TX interval, unit: one second */
    CTC_SC_OAM_MEP_PROP_APS_TX_RAPID,      /**< [TMM] Send APS rapidly. Valid when clear CTC_SC_OAM_PROP_HW_APS_SWTICH */
    CTC_SC_OAM_MEP_PROP_APS_INFO,          /**< [TMM] APS TX information, refer to ctc_sc_oam_aps_info_t */
    CTC_SC_OAM_MEP_PROP_APS_PROTECTION_EN, /**< [TMM] If set, use protection path. Valid when clear CTC_SC_OAM_PROP_HW_APS_SWTICH */
    CTC_SC_OAM_MEP_PROP_APS_CMD,           /**< [TMM] Set APS Command, refer to ctc_sc_oam_aps_cmd_t. Valid when set CTC_SC_OAM_PROP_HW_APS_SWTICH */
    CTC_SC_OAM_MEP_PROP_BER_CNT,           /**< [TMM] Get BIP and REI count refer to ctc_sc_oam_ber_cnt_t*/
    CTC_SC_OAM_MEP_PROP_MAX
};
typedef enum ctc_sc_oam_mep_prop_type_e ctc_sc_oam_mep_prop_type_t;

/**
 @brief   Define SPN Channel OAM property structure
*/
struct ctc_sc_oam_mep_prop_s
{
    uint32  client_id;              /**< [TMM] MEP key for SPN channel OAM */
    uint32  prop_type;              /**< [TMM] Refer to ctc_sc_oam_mep_prop_type_t */
    uint32  update_value;           /**< [TMM] Update value */
    void*   p_update_value;         /**< [TMM] Update values */
};
typedef struct ctc_sc_oam_mep_prop_s ctc_sc_oam_mep_prop_t;

/**
 @brief   Define SPN Channel OAM remote MEP data structure
*/
struct ctc_sc_oam_mep_info_s
{
    uint32 client_id;        /**< [TMM] MEP key for SPN channel OAM */

    /* Local status */
    uint8 first_rx;          /**< [TMM] Indicate received BAS, session is established */
    uint8 cs_lpi;            /**< [TMM] Value of CS_LPI */
    uint8 cs_lf;             /**< [TMM] Value of CS_LF */
    uint8 cs_rf;             /**< [TMM] Value of CS_RF */
    uint8 rdi;               /**< [TMM] Value of RDI */
    uint8 dloc;              /**< [TMM] Indicate status of dloc */
    uint8 ddeg;              /**< [TMM] Indicate status of ddeg */
    uint8 dexc;              /**< [TMM] Indicate status of dexc */
    uint8 sapi_chk;          /**< [TMM] If set, indicate SAPI check fail */
    uint8 dapi_chk;          /**< [TMM] If set, indicate DAPI check fail */
    uint8 cs_type_chk;       /**< [TMM] If set, indicate CS type check fail */
    uint8 seq_chk;           /**< [TMM] If set, indicate sequence number check fail */
    uint8 except_seq_num;    /**< [TMM] Except sequence number when check fail, used for CTC_OAM_DEFECT_SE_SEQ_CHK */

    /* Remote status */
    uint8 remote_cs_lpi;     /**< [TMM] Value of remote CS_LPI */
    uint8 remote_cs_lf;      /**< [TMM] Value of remote CS_LF */
    uint8 remote_cs_rf;      /**< [TMM] Value of remote CS_RF */
    uint8 remote_rdi;        /**< [TMM] Value of remote RDI */
    uint8 ddeg_rei;          /**< [TMM] Indicate status of ddeg for REI */
    uint8 dexc_rei;          /**< [TMM] Indicate status of dexc for REI */
    uint8 remote_period_chk; /**< [TMM] If set, indicate BAS remote period check fail */
};
typedef struct ctc_sc_oam_mep_info_s ctc_sc_oam_mep_info_t;

/**
 @brief Define OAM defect type for RDI & CPU
*/
enum ctc_sc_oam_defect_s
{
    CTC_SC_OAM_DEFECT_CSLPI                = (1<<0),  /**< [TMM] Remote CS_LPI */
    CTC_SC_OAM_DEFECT_CSLF                 = (1<<1),  /**< [TMM] Remote CS_LF */
    CTC_SC_OAM_DEFECT_CSRF                 = (1<<2),  /**< [TMM] Remote CS_RF */
    CTC_SC_OAM_DEFECT_RDI                  = (1<<3),  /**< [TMM] RX RDI */
    CTC_SC_OAM_DEFECT_DEXC                 = (1<<4),  /**< [TMM] Set dexc defect*/
    CTC_SC_OAM_DEFECT_DDEG                 = (1<<5),  /**< [TMM] Set ddeg defect*/
    CTC_SC_OAM_DEFECT_DEXC_REI             = (1<<6),  /**< [TMM] Set dexc defect for REI */
    CTC_SC_OAM_DEFECT_DDEG_REI             = (1<<7),  /**< [TMM] Set ddeg defect for REI */
    CTC_SC_OAM_DEFECT_DLOC                 = (1<<8),  /**< [TMM] Set dloc defect */
    CTC_SC_OAM_DEFECT_FIRST_RX             = (1<<9),  /**< [TMM] Receive BAS first time, means session established */
    CTC_SC_OAM_DEFECT_SEQ_CHK              = (1<<10), /**< [TMM] Sequence number check fail */
    CTC_SC_OAM_DEFECT_SAPI_CHK             = (1<<11), /**< [TMM] SAPI check fail */
    CTC_SC_OAM_DEFECT_DAPI_CHK             = (1<<12), /**< [TMM] DAPI check fail */
    CTC_SC_OAM_DEFECT_CS_TYPE_CHK          = (1<<13), /**< [TMM] CS type check fail */
    CTC_SC_OAM_DEFECT_PERIOD_CHK           = (1<<14), /**< [TMM] BAS RX period check fail */
    CTC_SC_OAM_DEFECT_CRC_CHK              = (1<<15), /**< [TMM] Rx CRC check fail*/
    CTC_SC_OAM_DEFECT_APS_MISMATCH         = (1<<16), /**< [TMM] APS mismatch such as A/B/D/R bit */
    CTC_SC_OAM_DEFECT_APS_STATE_CHANGE     = (1<<17), /**< [TMM] APS state change by remote request */
    CTC_SC_OAM_DEFECT_APS_PATH_SWITCH      = (1<<18)  /**< [TMM] APS path change */
};
typedef enum ctc_sc_oam_defect_s ctc_sc_oam_defect_t;

/**
 @brief   Define SPN Channel OAM property configure type
*/
enum ctc_sc_oam_prop_type_e
{
    CTC_SC_OAM_PROP_DEFECT_TO_CPU,           /**< [TMM] If corresponding defect set, the defect will be reported to CPU, refer to ctc_sc_oam_defect_t*/
    CTC_SC_OAM_PROP_DEFECT_TO_RDI,           /**< [TMM] If corresponding defect set, the defect will trigger RDI, refer to ctc_sc_oam_defect_t*/
    CTC_SC_OAM_PROP_EVENT_TO_CPU,            /**< [TMM] If corresponding event type set, the event will be reported to CPU, refer to ctc_sc_oam_event_type_t
                                                 only used for CTC_SC_OAM_EVENT_TYPE_APS_RX, CTC_SC_OAM_EVENT_TYPE_OTHER_PDU & CTC_SC_OAM_EVENT_TYPE_INVALID_PDU*/
    CTC_SC_OAM_PROP_HW_APS_SWTICH,           /**< [TMM] If set, run APS state machine and do APS switch automatically */
    CTC_SC_OAM_PROP_DEFECT_TO_APS_SWTICH,    /**< [TMM] If corresponding defect set, the defect will trigger APS switch, refer to ctc_sc_oam_defect_t*/
    CTC_SC_OAM_PROP_MAX
};
typedef enum ctc_sc_oam_prop_type_e ctc_sc_oam_prop_type_t;

/**
 @brief   Define SPN Channel OAM property data structure
*/
struct ctc_sc_oam_prop_s
{
    uint8    prop_type;  /**< [TMM] Config type, refer to ctc_sc_oam_prop_type_t*/
    uint32   value;      /**< [TMM] Configure value, set depend on cfg_type*/
};
typedef struct ctc_sc_oam_prop_s ctc_sc_oam_prop_t;

/**
 @brief Define timestamp format
*/
struct ctc_sc_oam_ts_s
{
    uint32  seconds;            /**< [TMM] seconds */
    uint32  nanoseconds;        /**< [TMM] nano seconds */
};
typedef struct ctc_sc_oam_ts_s ctc_sc_oam_ts_t;

/**
 @brief Define DM event structure
*/
struct ctc_sc_oam_dm_s
{
    uint8 dm_type;             /**< [TMM] 0: 1DM; 1: DMR */
    ctc_sc_oam_ts_t dm_delay;  /**< [TMM] DM delay */
};
typedef struct ctc_sc_oam_dm_s ctc_sc_oam_dm_t;

/**
 @brief Define event type
*/
enum ctc_sc_oam_event_type_s
{
    CTC_SC_OAM_EVENT_TYPE_DEFECT                = (1<<0), /**< [TMM] defect bmp changes, refer to ctc_sc_oam_defect_t */
    CTC_SC_OAM_EVENT_TYPE_APS_RX                = (1<<1), /**< [TMM] RX APS PDU */
    CTC_SC_OAM_EVENT_TYPE_DM_RX                 = (1<<2), /**< [TMM] RX DM PDU */
    CTC_SC_OAM_EVENT_TYPE_OTHER_PDU             = (1<<3), /**< [TMM] RX other PDU */
    CTC_SC_OAM_EVENT_TYPE_INVALID_PDU           = (1<<4)  /**< [TMM] Rx invalid PDU */
};
typedef enum ctc_sc_oam_event_type_s ctc_sc_oam_event_type_t;

/**
 @brief Define event data structure
*/
struct ctc_sc_oam_event_entry_s
{
    uint32 client_id;                  /**< [TMM] MEP key for SPN channel OAM */
    uint8  event_type;                 /**< [TMM] SC OAM event type, refer to ctc_sc_oam_event_type_t*/
    uint8  data_valid;                 /**< [TMM] If set to 1, means oam_data is valid, else oam_data is useless*/
    uint8  oam_data[8];                /**< [TMM] 64bits OAM data*/
    uint32 defect_bmp;                 /**< [TMM] Refer to ctc_sc_oam_defect_t, only valid when event_type is CTC_SC_OAM_EVENT_DEFECT */
    ctc_sc_oam_dm_t dm_event;          /**< [TMM] valid only when event_type is CTC_OAM_DEFECT_SE_DM_RX */
};
typedef struct ctc_sc_oam_event_entry_s ctc_sc_oam_event_entry_t;

/**
 @brief Define event data structure
*/
struct ctc_sc_oam_event_s
{
    uint16 valid_num;                                               /**< [TMM] Indicated valid event entry num */
    ctc_sc_oam_event_entry_t event_entry[CTC_SC_OAM_EVENT_MAX_NUM]; /**< [TMM] Event entry ,refer to ctc_sc_oam_event_entry_t */
};
typedef struct ctc_sc_oam_event_s ctc_sc_oam_event_t;
/**@} end of @defgroup  sc_oam SC_OAM */

#ifdef __cplusplus
}
#endif

#endif

